package com.minisoft.workhour;

import com.minisoft.workhour.util.JsonManhourSubmitter;

import java.util.Scanner;

/**
 * JSON工时数据提交工具 - 主类
 * 
 * 使用方法：
 * 1. 准备JSON格式的txt文件，包含工时数据数组
 * 2. 运行此工具，输入文件路径
 * 3. 工具会自动读取文件并提交到API
 * 
 * JSON文件格式示例：
 * [
 *   {
 *     "mode": "detailed",
 *     "owner": "MV8nFvTS",
 *     "task": "MV8nFvTSNxKwEv1j",
 *     "type": "recorded",
 *     "start_time": 1751515200,
 *     "hours": 400000,
 *     "description": "移动案场小程序企业客户修改需要增加Sap校验"
 *   }
 * ]
 */
public class JsonManhourMain {

    public static void main(String[] args) {
        System.out.println("=== JSON工时数据提交工具 ===");
        System.out.println("此工具用于读取JSON格式的工时数据文件并提交到API");
        
        JsonManhourSubmitter submitter = new JsonManhourSubmitter();
        Scanner scanner = new Scanner(System.in);
        
        while (true) {
            try {
                System.out.println("\n请选择操作：");
                System.out.println("1. 提交JSON工时文件");
                System.out.println("2. 查看JSON文件格式说明");
                System.out.println("3. 退出");
                System.out.print("请输入选择 (1-3): ");
                
                String choice = scanner.nextLine().trim();
                
                switch (choice) {
                    case "1":
                        handleSubmitFile(scanner, submitter);
                        break;
                    case "2":
                        showJsonFormat();
                        break;
                    case "3":
                        System.out.println("退出程序");
                        return;
                    default:
                        System.out.println("无效选择，请重新输入");
                        break;
                }
                
            } catch (Exception e) {
                System.err.println("操作异常: " + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    /**
     * 处理文件提交
     */
    private static void handleSubmitFile(Scanner scanner, JsonManhourSubmitter submitter) {
        System.out.print("请输入JSON文件路径: ");
        String filePath = scanner.nextLine().trim();
        
        if (filePath.isEmpty()) {
            System.out.println("文件路径不能为空");
            return;
        }
        
        // 如果是相对路径，转换为绝对路径
        if (!filePath.contains(":") && !filePath.startsWith("/")) {
            filePath = System.getProperty("user.dir") + "/" + filePath;
        }
        
        System.out.println("开始处理文件: " + filePath);
        
        try {
            JsonManhourSubmitter.SubmitResult result = submitter.submitFromJsonFile(filePath);
            
            System.out.println("\n=== 提交结果 ===");
            System.out.println("总记录数: " + result.getTotalCount());
            System.out.println("成功数量: " + result.getSuccessCount());
            System.out.println("失败数量: " + result.getFailureCount());
            System.out.println("成功率: " + String.format("%.1f%%", result.getSuccessRate()));
            
            if (result.isAllSuccess()) {
                System.out.println("✅ 所有记录提交成功！");
            } else {
                System.out.println("⚠️ 部分记录提交失败，请检查错误信息");
            }
            
        } catch (Exception e) {
            System.out.println("❌ 处理失败: " + e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 显示JSON文件格式说明
     */
    private static void showJsonFormat() {
        System.out.println("\n=== JSON文件格式说明 ===");
        System.out.println("JSON文件应该包含一个工时记录数组，每个记录包含以下字段：");
        System.out.println();
        System.out.println("字段说明：");
        System.out.println("- mode: 模式，通常为 \"detailed\"");
        System.out.println("- owner: 所有者ID，例如 \"MV8nFvTS\"");
        System.out.println("- task: 任务ID，例如 \"MV8nFvTSNxKwEv1j\"");
        System.out.println("- type: 类型，通常为 \"recorded\"");
        System.out.println("- start_time: 开始时间戳（秒），例如 1751515200");
        System.out.println("- hours: 工时（小时数*100000），例如 400000 表示4小时");
        System.out.println("- description: 工作描述");
        System.out.println();
        System.out.println("示例文件内容：");
        System.out.println("[");
        System.out.println("  {");
        System.out.println("    \"mode\": \"detailed\",");
        System.out.println("    \"owner\": \"MV8nFvTS\",");
        System.out.println("    \"task\": \"MV8nFvTSNxKwEv1j\",");
        System.out.println("    \"type\": \"recorded\",");
        System.out.println("    \"start_time\": 1751515200,");
        System.out.println("    \"hours\": 400000,");
        System.out.println("    \"description\": \"移动案场小程序企业客户修改需要增加Sap校验\"");
        System.out.println("  },");
        System.out.println("  {");
        System.out.println("    \"mode\": \"detailed\",");
        System.out.println("    \"owner\": \"MV8nFvTS\",");
        System.out.println("    \"task\": \"MV8nFvTSNxKwEv1j\",");
        System.out.println("    \"type\": \"recorded\",");
        System.out.println("    \"start_time\": 1751515200,");
        System.out.println("    \"hours\": 800000,");
        System.out.println("    \"description\": \"另一个工作任务\"");
        System.out.println("  }");
        System.out.println("]");
        System.out.println();
        System.out.println("注意：");
        System.out.println("1. 文件必须是有效的JSON格式");
        System.out.println("2. hours字段的值是小时数乘以100000，例如4小时=400000");
        System.out.println("3. start_time是Unix时间戳（秒）");
        System.out.println("4. 所有字符串字段都不能为空");
    }
}
